package com.example.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.List;

public class RateListActivity extends AppCompatActivity {

    private Handler handler;
    private ProgressBar bar;
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list);

        SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY, "");
        Log.i("List","lastRateDateStr=" + logDate);

        ListView mylist=findViewById(R.id.mylistview);
        //进度条,没数据时显示
        bar=findViewById(R.id.progressBar);
        //mylist.setEmptyView(bar);

//        String data[]={"one","two","three","测试数据项"};
//        ListAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
//        mylist.setAdapter(adapter);

        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if(msg.what==5){
                    List<String> list= (List<String>) msg.obj;
                    //用户用于刷新列表
                    ListAdapter adapter=new ArrayAdapter<String>(RateListActivity.this,android.R.layout.simple_list_item_1,list);
                    mylist.setAdapter(adapter);
                   bar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };
        //开启线程
        MyTask myTask= new MyTask();
        myTask.setHandler(handler);

//        RateActivity myTask=new RateActivity();
//        MyTask.setHandler(handler);
        Thread t = new Thread(myTask);
        t.start();//myTask.run();
    }
}